//
//  ViewController.m
//  XML数据解析
//
//  Created by li cheng on 16/8/11.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "GDataXMLNode.h"
#import "Album.h"
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *domAlbumArray;
@property (nonatomic, copy) NSString *element;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.domAlbumArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
    NSData *pathData = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    //1.创建GDataXMLDocument对象
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:pathData options:0 error:&error];
    //2.根据document获取根节点
    GDataXMLElement *root = [document rootElement];
    NSArray *rootElement = root.children;
    
    for (GDataXMLElement *childElement in rootElement) {
        _element = childElement.name;
        for (GDataXMLElement *sunElement in childElement.children) {
            Album *album = [Album new];
            if ([_element isEqualToString:@"page"]) {
                [album setValue:sunElement.stringValue forKey:sunElement.name];
            }
            [self.domAlbumArray addObject:album];
            for (GDataXMLElement *grandElement in sunElement.children) {
                [album setValue:grandElement.stringValue forKey:grandElement.name];
            }
        }
    }
    NSLog(@"%@", _domAlbumArray);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
